// *** LICENSE HEADER ***
// Filename: ./packetdata.cpp
// This file is part of Nodewarz 0.1
// Nodewarz is a 3d OpenGL strategy game
// Copyright (C) 2007 CZestmyr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// *** END LICENSE HEADER ***
#include "packetdata.h"

packetData::packetData(UDPpacket* thePacket){
    packet = thePacket;

    retriesLeft = SEREC_RETRY_NUMBER;
    waitTime = 0;
}

packetData::packetData(int packetSize){
    packet = SDLNet_AllocPacket(packetSize);
    packet->maxlen = packetSize;

    retriesLeft = SEREC_RETRY_NUMBER;
    waitTime = 0;
}

packetData::~packetData(){
    FreePacket();
}

packetData::packetData(packetData const& other){
    waitTime = other.waitTime;
    retriesLeft = other.retriesLeft;

    if (other.packet) {
        packet = SDLNet_AllocPacket(other.packet->len);
        packet->len = other.packet->len;
        packet->maxlen = other.packet->len;
        packet->address = other.packet->address;
        for (int i = 0; i < other.packet->len; ++i){
            packet->data[i] = other.packet->data[i];
        }
    } else packet = NULL;
}

packetData& packetData::operator=(packetData const& other){
    FreePacket();

    waitTime = other.waitTime;
    retriesLeft = other.retriesLeft;

    if (other.packet) {
        packet = SDLNet_AllocPacket(other.packet->len);
        packet->len = other.packet->len;
        packet->maxlen = other.packet->len;
        packet->address = other.packet->address;
        for (int i = 0; i < other.packet->len; ++i){
            packet->data[i] = other.packet->data[i];
        }
    } else packet = NULL;

    return *this;
}

void packetData::SetPacketNumber(Uint32 num){
    //Packet number is written into bytes 1-4 of the buffer
    SDLNet_Write32(num, &(packet->data[1]));
}

void packetData::SetPacketType(bool isData){
    //Packet type is in the byte 0 of the buffer
    if (isData)
        packet->data[0] = 1;
    else
        packet->data[0] = 0;
}

void packetData::SetMessage(const char* message){
    //Omit the first five bytes (packet type and number bytes)
    strcpy_safe_my((char*)&(packet->data[5]), message, packet->maxlen - 5);
    packet->len = strlen((char*)&(packet->data[5])) + 6;
}

void packetData::SetAddress(IPaddress addr){
    packet->address = addr;
}

bool packetData::IsData(){
    if (packet->data[0] == 1) return true;
    else return false;
}

Uint32 packetData::GetPacketNumber(){
    return SDLNet_Read32(&(packet->data[1]));
}

IPaddress packetData::GetAddress(){
    return packet->address;
}

UDPpacket* packetData::GetPacket(){
    return packet;
}

void packetData::FreePacket(){
    if (packet){
        SDLNet_FreePacket(packet);
        packet = NULL;
    }
}

void packetData::Update(int dt){
    waitTime -= dt;
}

int packetData::GetWaitTime(){
    return waitTime;
}

void packetData::NewTry(){
    waitTime = SEREC_ACK_WAIT_DELAY;
    --retriesLeft;
}

bool packetData::HasRetriesLeft(){
    /*if (retriesLeft > 0) return true;
    else return false;*/
    //We will force the packet to try to deliver indefinite number of times
    return true;
}
